﻿/**
 * http://www.cnblogs.com/anjou/archive/2008/05/25/1206832.html
 * 你參考 foxscraper 裡的 scraper class / httpclient class
可以直接拿  httpclient class 來用
然後直接看 GetSrc 這函式。
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using CsQuery;

namespace RedFoxUtils
{
    /// <summary>
    /// 支持 Session 和 Cookie 的 WebClient。
    /// </summary>
    public class HttpClient : WebClient
    {
        // Cookie 容器
        private CookieContainer cookieContainer;

        /// <summary>
        /// 創建一個新的 WebClient 實例。
        /// </summary>
        public HttpClient()
        {
            this.cookieContainer = new CookieContainer();
        }

        /// <summary>
        /// 創建一個新的 WebClient 實例。
        /// </summary>
        /// <param name="cookie">Cookie 容器</param>
        public HttpClient(CookieContainer cookies)
        {
            this.cookieContainer = cookies;
        }

        /// <summary>
        /// Cookie 容器
        /// </summary>
        public CookieContainer Cookies
        {
            get { return this.cookieContainer; }
            set { this.cookieContainer = value; }
        }

        /// <summary>
        /// 返回帶有 Cookie 的 HttpWebRequest。
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        //protected override WebRequest GetWebRequest(Uri address)
        //{
        //    WebRequest request = base.GetWebRequest(address);
        //    if (request is HttpWebRequest)
        //    {
        //        HttpWebRequest httpRequest = request as HttpWebRequest;
        //        httpRequest.CookieContainer = cookieContainer;
        //        httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)";
        //        httpRequest.KeepAlive = false;
        //    }
        //    return request;
        //}

        public void CQCreateFromUrl(string p_url)
        {
            //var dom = CQ.CreateFromUrl(p_url);
            byte[] responseData = this.DownloadData(p_url);
        }

        #region 封裝了PostData, GetSrc 和 GetFile 方法
        /// <summary>
        /// 向指定的 URL POST 數據，並返回頁面
        /// </summary>
        /// <param name="uriString">POST URL</param>
        /// <param name="postString">POST 的 數據</param>
        /// <param name="postStringEncoding">POST 數據的 CharSet</param>
        /// <param name="dataEncoding">頁面的 CharSet</param>
        /// <returns>頁面的源文件</returns>
        public string PostData(string uriString, string postString, string postStringEncoding, string dataEncoding, out string msg)
        {
            try
            {
                // 將 Post 字符串轉換成字節數組
                byte[] postData = Encoding.GetEncoding(postStringEncoding).GetBytes(postString);
                this.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                // 上傳數據，返回頁面的字節數組
                byte[] responseData = this.UploadData(uriString, "POST", postData);
                // 將返回的將字節數組轉換成字符串(HTML);
                string srcString = Encoding.GetEncoding(dataEncoding).GetString(responseData);
                srcString = srcString.Replace("\t", "");
                srcString = srcString.Replace("\r", "");
                srcString = srcString.Replace("\n", "");
                msg = string.Empty;
                return srcString;
            }
            catch (WebException we)
            {
                msg = we.Message;
                return string.Empty;
            }
        }

        /// <summary>
        /// 獲得指定 URL 的源文件
        /// </summary>
        /// <param name="uriString">頁面 URL</param>
        /// <param name="dataEncoding">頁面的 CharSet</param>
        /// <returns>頁面的源文件</returns>
        public string GetSrc(string uriString, string dataEncoding, out string msg)
        {
            try
            {
                // 返回頁面的字節數組
                byte[] responseData = this.DownloadData(uriString);
                // 將返回的將字節數組轉換成字符串(HTML);
                string srcString = Encoding.GetEncoding(dataEncoding).GetString(responseData);
                srcString = srcString.Replace("\t", "");
                srcString = srcString.Replace("\r", "");
                srcString = srcString.Replace("\n", "");
                msg = string.Empty;
                return srcString;
            }
            catch (WebException we)
            {
                msg = we.Message;
                return string.Empty;
            }
        }

        /// <summary>
        /// 從指定的 URL 下載文件到本地
        /// </summary>
        /// <param name="uriString">文件 URL</param>
        /// <param name="fileName">本地文件的完成路徑</param>
        /// <returns></returns>
        public bool GetFile(string urlString, string fileName, out string msg)
        {
            try
            {
                this.DownloadFile(urlString, fileName);
                msg = string.Empty;
                return true;
            }
            catch (WebException we)
            {
                msg = we.Message;
                return false;
            }
        }
        #endregion
    }
}
